package com.qf.controller;

import com.qf.entity.MyJson;
import com.qf.entity.SystemMnue;
import com.qf.entity.UserEmp;
import com.qf.entity.UserRole;
import com.qf.mapper.UserMapper;
import com.qf.service.UserLoginExitService;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 2019/11/22
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */
@Controller
public class LoginController {

    @Autowired
    private UserLoginExitService userLoginExitService;
    @Resource
    private UserMapper userMapper;

    //登录失败时处理异常的代码
    @RequestMapping("/login")
    public String login(HttpServletRequest request,HttpServletResponse response) {
        System.out.println("认证失败！！！");
        //从from获得验证失败的信息
        String failure = (String) request.getAttribute("shiroLoginFailure");
        MyJson json = new MyJson();
        json.setMessage("用户名或密码错误");

        if (failure != null) {
            if (UnknownAccountException.class.getName().equals(failure)) {

                try {
                    throw new RuntimeException("用户名异常");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    return "login";
                }

            } else if (IncorrectCredentialsException.class.getName().equals(failure)) {
                try {
                    throw new RuntimeException("密码错误");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    return "login";
                }
            } else {

                try {
                    throw new RuntimeException("未知错误");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    return "login";

                }
            }

        }
        return "login";
    }

 /*       //退出系统
   @RequestMapping("/exit")
   public String exit (HttpSession session) {
            session.invalidate();
            return "redirect:/login.jsp";
    }*/

}
